package com.sc.nft.hna.form;

import cn.hutool.core.lang.Assert;
import cn.hutool.core.util.NumberUtil;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.sc.nft.config.hna.HnaConfig;
import com.sc.nft.exception.GlobalRunTimeException;
import lombok.Data;

import java.util.Map;

@Data
public class T003TransForm extends BaseTransForm {

  public static final String[] encryptArr =
      new String[] {"payUserId", "receiveUserId", "tranAmount", "businessType"};
  public static final String[] verifyArr =
      new String[] {
        "version",
        "tranCode",
        "merOrderId",
        "merId",
        "charset",
        "signType",
        "resultCode",
        "errorCode"
      };
  public static final String[] submitArr =
      new String[] {
        "version",
        "tranCode",
        "merId",
        "merOrderId",
        "submitTime",
        "msgCiphertext",
        "signType",
        "signValue",
        "merAttach",
        "charset"
      };
  private static final long serialVersionUID = 957799666769085106L;

  private String payUserId; // 付款方用户编号
  private String receiveUserId; // 收款方ID
  private String tranAmount; // 转账金额
  private String businessType; // 业务类型

  public static final String MAIN_URL = "https://ncount.hnapay.com/api/t003.htm";
  public static final String TRAN_CODE = "T003";

  @Override
  public String getEncryptJsonStr() {
    return getJsonStr(this, encryptArr);
  }

  @Override
  public String getVerifyJsonStr() {
    return getJsonStr(this, verifyArr);
  }

  @Override
  public String getSubmitJsonStr() {
    return getJsonStr(this, submitArr);
  }

  public static Map<String, Object> generateParam(
      HnaConfig hnaConfig, String userId, String tranAmount) {
    T003TransForm transForm = new T003TransForm();
    transForm.initCommonParams(TRAN_CODE, hnaConfig.getMerId(), IdWorker.getIdStr(), "");
    Assert.isTrue(NumberUtil.isNumber(tranAmount), () -> new GlobalRunTimeException("转账金额必须是数字"));
    transForm.setReceiveUserId(userId);
    transForm.setPayUserId(hnaConfig.getMerId());
    transForm.setTranAmount(tranAmount);
    return generateParam(transForm, hnaConfig);
  }

  public static Map<String, Object> customGenerateParam(
      HnaConfig hnaConfig, String from, String to, String tranAmount) {
    T003TransForm transForm = new T003TransForm();
    transForm.initCommonParams(TRAN_CODE, hnaConfig.getMerId(), IdWorker.getIdStr(), "");
    Assert.isTrue(NumberUtil.isNumber(tranAmount), () -> new GlobalRunTimeException("转账金额必须是数字"));
    transForm.setReceiveUserId(to);
    transForm.setPayUserId(from);
    transForm.setTranAmount(tranAmount);
    return generateParam(transForm, hnaConfig);
  }
}
